﻿using Business.AssembleInfoManagement.Dto;
using Business.AssProManagement.Dto;
using Business.BookManagement;
using Business.BookManagement.Dto;
using Business.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text; 
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using XCZ;

namespace Business.AssembleInfoManagement
{
    public class AssembleInfoAppService : BusinessWorkFlowAppService, IAssembleInfoAppService
    {
        private const string FormName = "AssembleInfo";
        private IRepository<AssembleInfo, Guid> _repository;
        private IRepository<AssPro, Guid> _repository1;
        public AssembleInfoAppService(
            IRepository<AssembleInfo, Guid> repository,
            IRepository<AssPro, Guid> repository1
            )
        {
            _repository = repository;
            _repository1 = repository1;
        }
        #region 增删改查基础方法

        public async Task<AssembleInfoDto> Get(Guid id)
        {
            var data = await _repository.GetAsync(id);
            var dto = ObjectMapper.Map<AssembleInfo, AssembleInfoDto>(data);
            return dto;
        }

        public async Task<PagedResultDto<AssembleInfoDto>> GetAll(GetAssembleInfoInputDto input,string dadname,string Assemblenumber)
        {
            var query = (from a in await _repository.GetQueryableAsync()
                        join b in await _repository1.GetQueryableAsync() on a.Id.ToString() equals b.tid
                        select new AssembleInfoDto
                        { 
                             Id = a.Id,
                             Assemblenumber = a.Assemblenumber,
                             dadname = a.dadname,
                             dadnum = a.dadnum,
                             dadwei = a.dadwei,
                             zdDesc = a.zdDesc,
                             zdname = a.zdname,
                             zdtime = a.zdtime,
                        }).Distinct().ToList();

            if (!string.IsNullOrEmpty(dadname))
            {
                query = query.Where(x => x.dadname.Contains(dadname)).ToList();
            }
            if (!string.IsNullOrEmpty(Assemblenumber))
            {
                query = query.Where(x=>x.Assemblenumber.Contains(Assemblenumber)).ToList();
            }

            var totalCount =  query.Count();
            query = query.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();
            return new PagedResultDto<AssembleInfoDto>(totalCount, query);
        }
        public async Task<AssembleInfoDto> CreateOrUpdate(CreateOrUpdateAssembleInfoDto input)
        {
            AssembleInfo result = null;
            Random random=new Random(); 
            if (!input.Id.HasValue)
            {
                input.Id = GuidGenerator.Create();
                input.Assemblenumber = "JH" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + random.Next(1000,10000);
                input.zdtime = DateTime.Now;
                AssembleInfo assemble = new AssembleInfo()
                {
                     Assemblenumber = input.Assemblenumber,
                     zdtime=DateTime.Now,
                     dadname = input.zdname,
                     dadwei=input.dadwei,
                     dadnum=input.dadnum,
                     zdDesc=input.zdDesc,
                     zdname=input.zdname,
                     IsDeleted = false,
                };
                foreach (var item in input.ProduceAsspro)
                {
                    AssPro asspro = new AssPro()
                    {
                        tid = Convert.ToString(input.Id),
                        cpname = item.cpname,
                        cpdw = item.cpdw,
                        cpnum = item.cpnum,
                        cpnumber = item.cpnumber,
                        cptype = item.cptype,
                        cpprice = item.cpprice,
                        IsDeleted = false
                    };
                    await _repository1.InsertManyAsync(new List<AssPro>() { asspro });
                }

                result = await _repository.InsertAsync(ObjectMapper.Map<CreateOrUpdateAssembleInfoDto, AssembleInfo>(input));
            }
            else
            {
                var data = await _repository.GetAsync(input.Id.Value);
                AssembleInfo assemble = new AssembleInfo()
                {
                    Assemblenumber = input.Assemblenumber,
                    zdtime = DateTime.Now,
                    dadname = input.zdname,
                    dadwei = input.dadwei,
                    dadnum = input.dadnum,
                    zdDesc = input.zdDesc,
                    zdname = input.zdname,
                    IsDeleted = false,
                };
                foreach (var item in input.ProduceAsspro)
                {
                    AssPro asspro = new AssPro()
                    {
                        tid = Convert.ToString(input.Id),
                        cpname = item.cpname,
                        cpdw = item.cpdw,
                        cpnum = item.cpnum,
                        cpnumber = item.cpnumber,
                        cptype = item.cptype,
                        cpprice = item.cpprice,
                        IsDeleted = false
                    };
                    await _repository1.InsertManyAsync(new List<AssPro>() { asspro });
                }
                result = await _repository.InsertAsync(ObjectMapper.Map(input,data));
            }
            return ObjectMapper.Map<AssembleInfo, AssembleInfoDto>(result);
        }

        public async Task Delete(List<Guid> ids)
        {
            foreach (var item in ids)
            {
                await _repository.DeleteAsync(item);
            }

        }

        public async Task<CreateOrUpdateAssembleInfoDto> GetAssembleInfo(string id)
        {
            var detail = await _repository1.GetQueryableAsync();
            var data = (from a in await _repository.GetQueryableAsync()
                        where a.Id.ToString() == id
                        select new CreateOrUpdateAssembleInfoDto()
                        {
                            Id = a.Id,
                            Assemblenumber = a.Assemblenumber,
                            dadname = a.dadname,
                            dadnum = a.dadnum,
                            dadwei = a.dadwei,
                            zdDesc = a.zdDesc,
                            zdname = a.zdname,
                            zdtime = a.zdtime,
                            ProduceAsspro = (from b in detail
                                             where a.Id.ToString() == b.tid
                                             select new AssProDto()
                                             {
                                                 tid = b.tid,
                                                 Id = b.Id,
                                                 cpdw = b.cpdw,
                                                 cpname = b.cpname,
                                                 cpnum = b.cpnum,
                                                 cpnumber = b.cpnumber,
                                                 cpprice = b.cpprice,
                                                 cptype = b.cptype,
                                             }).ToList()
                        }).FirstOrDefault();
            return data;
        }
        #endregion
    }
}
